<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        placeholder="用户名"
        style="width: 200px;"
        class="filter-item"
      />
      <el-select
        v-model="listQuery.roleId"
        placeholder="角色"
        clearable
        style="width:130px"
        class="filter-item"
      >
        <el-option
          v-for="role in rolesData"
          :key="role.id"
          :label="role.roleName"
          :value="role.id"
        />
      </el-select>
      <el-select
        v-model="listQuery.userStatus"
        placeholder="用户状态"
        :clearable="false"
        style="width:130px"
        class="filter-item"
      >
        <el-option
          v-for="userStatus in statusData"
          :key="userStatus.key"
          :label="userStatus.value"
          :value="userStatus.key"
        />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column align="center" label="编号" min-width="60">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="120px" align="center" label="用户名" prop="userLoginAccount">
        <!-- <template slot-scope="{row}">
          <router-link :to="'edit/'+row.id" class="link-type">
          <span>{{ row.username }}</span>
          </router-link>
        </template>-->
      </el-table-column>

      <el-table-column min-width="120px" align="center" label="真实姓名" prop="userName">
        <!-- <template slot-scope="scope">
          <span>{{ scope.row.realname }}</span>
        </template>-->
      </el-table-column>

      <el-table-column
        min-width="300px"
        align="center"
        label="角色"
        prop="role"
        :formatter="this.roleFormatter"
      >
        <!-- <template slot-scope="scope">
          <span>{{ scope.row.role }}</span>
        </template>-->
      </el-table-column>

      <el-table-column
        min-width="300px"
        align="center"
        label="是否主管"
        prop="orgChiefFlag"
        :formatter="this.orgChiefFlagFormatter"
      >
        <!-- <template slot-scope="scope">
          <span>{{ scope.row.role }}</span>
        </template>-->
      </el-table-column>

      <el-table-column min-width="150px" align="center" label="联系电话" prop="userTelNumber">
        <!-- <template slot-scope="scope">
          <span>{{ scope.row.telephone }}</span>
        </template>-->
      </el-table-column>
      <!-- <el-table-column width="120px" align="center" label="邮箱" prop="email"> -->
      <!-- <template slot-scope="scope">
          <span>{{ scope.row.email }}</span>
      </template>-->
      <!-- </el-table-column> -->
      <!-- <el-table-column width="120px" align="center" label="QQ" prop="qq"> -->
      <!-- <template slot-scope="scope">
          <span>{{ scope.row.qq }}</span>
      </template>-->
      <!-- </el-table-column> -->
      <el-table-column min-width="150px" align="center" label="所属组织" prop="strOrgName">
        <!-- <template slot-scope="scope">
          <span>{{ scope.row.organization }}</span>
        </template>-->
      </el-table-column>
      <el-table-column
        min-width="150px"
        align="center"
        label="用户状态"
        prop="userStatus"
        :formatter="statusFormatter"
      ></el-table-column>
      <!-- <el-table-column width="120px" align="center" label="岗位" prop="post"> -->
      <!-- <template slot-scope="scope">
          <span>{{ scope.row.post }}</span>
      </template>-->
      <!-- </el-table-column> -->
      <!-- <el-table-column width="120px" align="center" label="职位" prop="work"> -->
      <!-- <template slot-scope="scope">
          <span>{{ scope.row.work }}</span>
      </template>-->
      <!-- </el-table-column> -->

      <!-- <el-table-column class-name="status-col" label="登录日期" width="100" prop="loginDate"> -->
      <!-- <template slot-scope="scope">
          <span>{{ scope.row.loginDate}}</span>
      </template>-->
      <!-- </el-table-column> -->
      <!-- <el-table-column class-name="status-col" label="注册日期" width="100" prop="registerDate"> -->
      <!-- <template slot-scope="scope">
          <span>{{ scope.row.registerDate}}</span>
      </template>-->
      <!-- </el-table-column> -->

      <el-table-column align="center" label="操作" fixed="right" min-width="200px">
        <template slot-scope="scope">
          <router-link :to="'edit/'+scope.row.id" v-if="scope.row.userStatus==1">
            <el-button type="primary" size="small" icon="el-icon-edit">编辑</el-button>
          </router-link>
          <!-- <router-link :to="'edit/'+scope.row.id">
            <el-button size="small" icon="el-icon-view">查看</el-button>
          </router-link>-->
          <el-button
            type="danger"
            size="small"
            icon="el-icon-close"
            v-if="scope.row.userStatus==1"
            @click="deleteUser(scope.row.id)"
          >禁用</el-button>
          <el-button
            type="success"
            size="small"
            icon="el-icon-check"
            v-if="scope.row.userStatus==0"
            @click="enableUser(scope.row.id)"
          >启用</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageIndex"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { fetchList, deleteUser, enableUser } from '@/api/user'
import { getRoles } from '@/api/role'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'UserList',
  components: { Pagination },
  data() {
    return {
      rolesData: [], //角色列表
      statusData: [
        { key: 1, value: '正常' },
        { key: 0, value: '无效' }
      ],
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageIndex: 1,
        limit: 10,
        name: '',
        roleId: '',
        userStatus: 1
      }
    }
  },
  created() {
    this.getList()
    this.getRolesList()
  },
  methods: {
    statusFormatter(row, colum) {
      if (row.userStatus === 1) {
        return '正常'
      } else {
        return '无效'
      }
    },
    roleFormatter(row, column) {
      // if (row.role === 'admin') {
      //   return '管理员'
      // } else if (row.role === 'enterpriseUser') {
      //   return '企业用户'
      // }
      let result = ''
      row.roles.forEach((role) => {
        result += role.roleName + ','
      })
      return result.substring(0, result.length - 1)
    },
    orgChiefFlagFormatter(row, colum) {
      if (row.orgChiefFlag === 1) {
        return '是'
      } else {
        return '否'
      }
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then((response) => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    getRolesList() {
      getRoles(1).then((response) => {
        // console.log('roles---------------', response.data)
        this.rolesData = response.data
      })
    },
    enableUser(userId) {
      this.$confirm('此操作将永久启用该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        enableUser({ userId }).then((res) => {
          if (res.code == 20000) {
            //删除成功，重新加载数据
            this.getList()
            this.$message({
              type: 'success',
              message: '启用成功!'
            })
          } else {
            this.$message({
              type: 'success',
              message: '启用失败!'
            })
          }
        })
      })
    },
    deleteUser(userId) {
      this.$confirm('此操作将永久禁用该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          //发请求删除用户
          deleteUser({ userId }).then((res) => {
            if (res.code == 20000) {
              //删除成功，重新加载数据
              this.getList()
              this.$message({
                type: 'success',
                message: '禁用成功!'
              })
            } else {
              this.$message({
                type: 'success',
                message: '禁用失败!'
              })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    handleFilter() {
      this.getList()
    }
  }
}
</script>

<style scoped  lang="scss" >
.app-container /deep/ .pagination-container {
  margin-top: 0;
  padding: 16px 16px;
}
@import '~@/styles/mixin.scss';
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
.createPost-container {
  position: relative;

  .createPost-main-container {
    padding: 40px 45px 20px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}
</style>

